package com.keyingbo.websocket.utils;

import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
 * token工具类
 */
public class TokenUtil {
    private static ConcurrentHashMap<String , String> userTokenMap = new ConcurrentHashMap<>();//userId-->token 一般存在数据库中
    private static ConcurrentHashMap<String , String> tokenUserMap = new ConcurrentHashMap<>();//token->userId 一般存在redis缓存

    /**
     * 设置token
     * @param userId
     */
    public static void resetUserAndToken(String userId ) {
        String token = UUID.randomUUID().toString();
        userTokenMap.put(userId , token);
        tokenUserMap.put(token , userId);
    }

    /**
     * 获取token
     * @param userId
     * @return
     */
    public static String getToken(String userId) {
        return userTokenMap.get(userId);
    }

    /**
     * 通过token获取userId
     * @param token
     * @return
     */
    public static String getUserId (String token) {
        return tokenUserMap.get(token);
    }
}
